/**
 * jims
 */
package com.jims.master.modules.blood.service;


import com.jims.master.common.config.Global;
import com.jims.master.common.web.impl.BaseDto;
import com.jims.master.modules.finance.dao.InpBillDetailDao;
import com.jims.master.modules.finance.entity.InpBillDetail;
import com.jims.master.modules.sys.utils.UserUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import com.jims.master.common.service.CrudService;
import com.jims.master.modules.blood.entity.BloodTest;
import com.jims.master.modules.blood.dao.BloodTestDao;

/**
 * 验血表Service
 * @author CTQ
 * @version 2017-04-11
 */
@Service
@Transactional(readOnly = true)
public class BloodTestService extends CrudService<BloodTestDao, BloodTest> {

	@Autowired
	private InpBillDetailDao inpBillDetailDao;

	/**
	 * 查询最大申请单号
	 * @param bloodTest
	 * @return
	 */
	public String findMaxTestNo(BloodTest bloodTest){
		bloodTest.updateOrgId();
		return dao.findMaxTestNo(bloodTest);
	}
	/**
	 * 根据参数查询验血信息
	 * @param bloodTest
	 * @return
	 */
	public BloodTest findTestDataByParams(BloodTest bloodTest){
		return dao.findTestDataByParams(bloodTest);
	}
	
	@Transactional(readOnly = false)
	public int saveTest(BloodTest bloodTest) {
		bloodTest.updateOrgId();
		int num = 0;
		if (bloodTest.getTestFeeFlag().equals(Global.YES)){//收取验血费
			//查询验血费所对应收费项目，插入到inp_bill_detail表
			BaseDto baseDto = dao.findTestFee(bloodTest);
			if (baseDto!=null){
				InpBillDetail bd = new InpBillDetail();
				bd.setVisitId(bloodTest.getVisitId());
				bd.setPatientId(bloodTest.getPatientId());
				bd.setAmount(baseDto.getAsString("amount"));
				bd.setCharges(baseDto.getAsDouble("charges"));
				bd.setClassOnMr(baseDto.getAsString("class_on_mr"));
				bd.setClassOnReckoning(baseDto.getAsString("class_on_reckoning"));
				bd.setCosts(baseDto.getAsDouble("costs"));
				bd.setDeptStayed(bloodTest.getDeptStayed().getId());
				bd.setItemClass(baseDto.getAsString("item_class"));
				bd.setItemName(baseDto.getAsString("item_name"));
				bd.setItemPrice(baseDto.getAsDouble("price"));
				bd.setItemSpec(baseDto.getAsString("item_spec"));
				bd.setPriceId(baseDto.getAsString("id"));
				bd.setSubjCode(baseDto.getAsString("subj_code"));
				bd.setPriceQuotiety("1");
				bd.setUnits(baseDto.getAsString("units"));
				bd.setWardCode(bloodTest.getWardCode());
				bd.setOrderDoctor(UserUtils.getUser().getId());
				bd.setOrderDept(UserUtils.getUser().getOrgDept()!=null?UserUtils.getUser().getOrgDept().getId():"");
				bd.setPerformPerson(UserUtils.getUser().getId());
				bd.setPerformDept(UserUtils.getUser().getOrgDept()!=null?UserUtils.getUser().getOrgDept().getId():"");
				bd.preInsert();
				bd.updateOrgId();
				inpBillDetailDao.insert(bd);
			}
		}
		num = super.save(bloodTest);
		return num;
	}
	
	@Transactional(readOnly = false)
	public int delete(BloodTest bloodTest) {
		return super.delete(bloodTest);
	}
	
}